package com.niuyubo;

import com.common.collection.Student;
import com.common.collection.Classes;

import java.util.*;


public class StudentDemo1 {

    public static void main(String[] args) {
        filterByAge();
        distinctName();
        nameTimes();
        sortByAge();
        addressAndInfo();
    }

    static void filterByAge() {
        Classes classes = new Classes();
        List<Student> stlist = classes.students();
        stlist.forEach(student -> {
            if (student.getAge() < 15) {
                System.out.println(student);
            }
        });
    }

    static void distinctName() {
        Classes classes = new Classes();
        List<Student> stlist = classes.students();
        Set<String> names =new HashSet<>();
        stlist.forEach(student -> {
            String name = student.getName();
            names.add(name);
        });
        names.forEach(name -> {
            System.out.println(name);
        });
    }


    static void distinctName1() {
        Classes classes = new Classes();
        List<Student> stlist = classes.students();
        List<String> names = new ArrayList<>();
        stlist.forEach(student -> {
            names.add(student.getName());
        });
        Set<String> disnames = new HashSet<>(names);
        names.forEach(name -> {
            System.out.println(name);
        });
    }

    static void nameTimes() {
        Classes classes = new Classes();
        List<Student> stlist = classes.students();
        Map<String, Integer> map = new HashMap<>();
        stlist.forEach(student -> {
            String name = student.getName();
            Integer count = map.get(name);
            if(Objects.isNull(count)) {
                map.put(name, 1);
            }else{
                map.put(name, count +1);
            }
        });
        for(Map.Entry<String, Integer> entry: map.entrySet()) {
            System.out.println(entry.getKey() + ":" + entry.getValue());
        }
    }


    static void nameTimes1() {
        Classes classes = new Classes();
        List<Student> stlist = classes.students();
        List<String> names = new ArrayList<>();
        stlist.forEach(student -> {
            String name = student.getName();
            names.add(name);
        });
        Set<String> disName = new HashSet<>(names);
        disName.forEach(disname -> {
            int count = 0;
            for (String name : names) {
                if (name.equals(disname)) {
                    count++;
                }
            }
            System.out.println(disname + ":" + count);
        });
    }


    static void sortByAge() {
        Classes classes = new Classes();
        List<Student> stlist = classes.students();
        Map<Integer, Student> sortMap = new HashMap<>();
        for (Student student : stlist) {
            sortMap.put(student.getAge(), student);
        }
        for(int i = 1; i < 100; i++) {
            Student student = sortMap.get(i);
            if(Objects.nonNull(student)){
                System.out.println(student);
            }
        }


    }

    //冒泡排序
    static void sortByAge1() {
        Classes classes = new Classes();
        List<Student> stlist = classes.students();
        for (int i = 0; i <stlist.size(); i++) {
            for (int j = 0; j <stlist.size()-1; j++) {
                Student sj = stlist.get(j);
                Student sj1 = stlist.get(j+1);
                if (sj.getAge() > sj1.getAge()) {
                    stlist.set(j+1, sj);
                    stlist.set(j, sj1);
                }
            }
        }
        stlist.forEach(student -> {
            System.out.println(student);
        });

    }

    //比较器接口
    static void sortByAge2() {
        Classes classes = new Classes();
        List<Student> stlist = classes.students();
        Comparator<Student> comparator = (stu1, stu2) -> {
            Integer age1 = stu1.getAge(), age2 = stu2.getAge();
            return age1 - age2;
        };
        stlist.sort(comparator);
        stlist.forEach(student -> {System.out.println(student);});
    }

    static void addressAndInfo() {
        Classes classes = new Classes();
        List<Student> stlist = classes.students();
        Map<String, List<Student>> mapadd = new HashMap<>();
        stlist.forEach(student -> {
            String address = student.getAddress();
            List<Student> students = mapadd.get(address);
            if (Objects.isNull(students)) {
                students = new ArrayList<>();
                mapadd.put(address, students);
            }
            students.add(student);
        });
        for (Map.Entry<String, List<Student>> entry : mapadd.entrySet()) {
            System.out.print(entry.getKey() + "(" + entry.getValue() + ")" + "\n");
        }
    }

}
